import javax.swing.*;
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableColumn;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;


public class ResultGui extends JFrame implements ActionListener
{
	//Singleton
	JButton btnExit;
	JLabel lblStudentName,lblStudentNameVal;
	JPanel screenSub,screenMain;
	
	JTable jTResult;
	
	private static ResultGui rg=null;
	
	private ResultGui(Student s,int depth)
	{

		ResultSheetOperation.getInstance().toggleAllButtonsStatus(false);
		
		//Labels
		Font fontLbl=new Font(ProgramUitl.FONT_NAME,
									ProgramUitl.NORMAL_FONT_STLYE,
									ProgramUitl.NORMAL_FONT_SIZE);
		lblStudentName=new JLabel(ProgramUitl.STUDNET_NAME_TEXT);						
		lblStudentName.setFont(fontLbl);
		
		lblStudentNameVal=new JLabel(s.getName());						
		lblStudentNameVal.setFont(fontLbl);
		//		
		
		//Buttons
		btnExit=new JButton(ProgramUitl.EXIT_TEXT,ProgramUitl.getButtonIcon(ProgramUitl.ICON_TYPE.ERROR));
		btnExit.addActionListener(this);		
		//		
		
		//Panel
		screenMain= (JPanel)getContentPane( );
		screenMain.setLayout( new BorderLayout() );		
		screenMain.setBorder(new EmptyBorder(10, 10, 10, 10));
		//

		if(populateTable(s,depth))
		{

			screenMain.add("Center",new JScrollPane(jTResult));
		}
	
		
		//Frame Settings
		addWindowListener(new WindowListener() {			
			public void windowClosing(WindowEvent e){exit();}
			public void windowOpened(WindowEvent e) 
			{
				MainMenu.getInstance().setVisible(false);
				MainMenu.getInstance().setEnabled(false);
			}			       
			public void windowClosed(WindowEvent e) {}
			public void windowIconified(WindowEvent e) {}
			public void windowDeiconified(WindowEvent e) {}
			public void windowActivated(WindowEvent e) {}
			public void windowDeactivated(WindowEvent e) {}
		});

		
		setSize(ProgramUitl.MEDIUM_SCREEN_2_WIDTH,ProgramUitl.MEDIUM_SCREEN_HEIGHT);
		setLocation(ProgramUitl.SCREEN_LOCATION_TOP,ProgramUitl.SCREEN_LOCATION_LEFT);
		setLocationRelativeTo(null);
		setTitle(ProgramUitl.HEADNING_TEXT);		
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);		
		setUndecorated(false);			
		setVisible(true);		
		validate();
		//
	}		
	
	public static ResultGui getInstance(Student s,int depth)
	{
		if(rg==null)
		{
			rg=new ResultGui(s,depth);
		}
		
		return rg;
	}	
	//

	//	
	//
	
	//Methods
	boolean populateTable(Student s,int depth)
	{
		java.util.List<Enroll> eCore=null;
		eCore=XmlOperations.getInstance().getStudentEnrolInfoByCourseType(Integer.toString(s.getNo()),ProgramUitl.COURSE_TYPE.CORE);
		
		java.util.List<Enroll> eOptional=null;
		eOptional=XmlOperations.getInstance().getStudentEnrolInfoByCourseType(Integer.toString(s.getNo()),ProgramUitl.COURSE_TYPE.OPTIONAL);
		
		java.util.List<Enroll> eProject=null;		
		if(ProgramUitl.DEPTH_WITH_PRAC!=depth)
		{			
			eProject=XmlOperations.getInstance().getStudentEnrolInfoByCourseType(Integer.toString(s.getNo()),ProgramUitl.COURSE_TYPE.PROJECT);
		}
		
		java.util.List<Enroll> singleList=new ArrayList<Enroll>();
		
		singleList=getASingleList(eCore,singleList);
		singleList=getASingleList(eOptional,singleList);
		singleList=getASingleList(eProject,singleList);
		
		int columnC = 1;		
		DefaultTableModel dtm=new javax.swing.table.DefaultTableModel(new Vector(), new Vector());
		
		//Cols
		if(eCore!=null){columnC+=(eCore.size() *2);}	
		if(eOptional!=null){columnC+=(eOptional.size() *2);}					
		if(eProject!=null){columnC+=(eProject.size() *2);}
		
			
		String[] cols=new String[columnC];
		String[][] rows=new String[depth][columnC];
		
		rows[0][0]=ProgramUitl.EMPTY;
		if(singleList!=null)
		{
			int i=1,j=1;
			while(i<=singleList.size())
			{

				Enroll eTmp=(Enroll)singleList.get(i-1);
				Course c=XmlOperations.getInstance().searchCourseByNo(Integer.toString(eTmp.getCode()));	
				rows[0][j]=c.getName();		
				j++;
				rows[0][j]=ProgramUitl.EMPTY;
				j++;
				
				i++;				
			}
		}
		
		rows[1][0]=ProgramUitl.EMPTY;
		for(int i=1;i<columnC;i++)
		{
				rows[1][i]=(i%2==0) ? ProgramUitl.MARK_REPEAT_TEXT:ProgramUitl.MARK_NORMAL_TEXT;
		}
			
		if(ProgramUitl.DEPTH_WITH_PRAC==depth)
		{	
			rows[2][0]=ProgramUitl.MARK_ASSIGN_1_TEXT;
			rows[3][0]=ProgramUitl.MARK_ASSIGN_2_TEXT;
			rows[4][0]=ProgramUitl.MARK_ASSIGN_3_TEXT;
			rows[5][0]=ProgramUitl.MARK_CLASS_TEST_TEXT;
			rows[6][0]=ProgramUitl.MARK_FINAL_PAPER_TEXT;
			rows[7][0]=ProgramUitl.MARK_PROJECT_INTERIM_REPORT_TEXT;
			
			
		}
		else
		{			
			rows[2][0]=ProgramUitl.MARK_ASSIGN_1_TEXT;
			rows[3][0]=ProgramUitl.MARK_ASSIGN_2_TEXT;
			rows[4][0]=ProgramUitl.MARK_ASSIGN_3_TEXT;
			rows[5][0]=ProgramUitl.MARK_CLASS_TEST_TEXT;
			rows[6][0]=ProgramUitl.MARK_FINAL_PAPER_TEXT;
			rows[7][0]=ProgramUitl.MARK_PROJECT_INTERIM_REPORT_TEXT;
			rows[8][0]=ProgramUitl.MARK_PROJECT_INTERIM_PRESENTATION_TEXT;
			rows[9][0]=ProgramUitl.MARK_PROJECT_FINAL_REPORT_TEXT;
			rows[10][0]=ProgramUitl.MARK_PROJECT_FINAL_PRESENTATION_TEXT;
			rows[11][0]=ProgramUitl.MARK_PROJECT_FINAL_PRODUCT_TEXT;	
		}
		
		int j=0;
		cols[j]=ProgramUitl.EMPTY;
		j++;
		if(eCore!=null)
		{		
			for(int i=0;i<(eCore.size() *2);i++)
			{	
				if(i==0){cols[j]=ProgramUitl.COURSE_TYPE__CORE_TEXT;}		
				else{cols[j]=ProgramUitl.EMPTY;}
				j++;
			}
			//
		}	
		if(eOptional!=null)
		{
			for(int i=0;i<(eOptional.size() *2);i++)
			{	
				if(i==0){cols[j]=ProgramUitl.COURSE_TYPE__OPTIONAL_TEXT;}		
				else{cols[j]=ProgramUitl.EMPTY;}				
				j++;
			}			
		}
		
		if(eProject!=null)
		{
			for(int i=0;i<(eProject.size() *2);i++)
			{	
				if(i==0){cols[j]=ProgramUitl.COURSE_TYPE__PROJECT_TEXT;}		
				else{cols[j]=ProgramUitl.EMPTY;}				
				j++;
			}			
		}	

		//
		
		if(singleList!=null)
		{
			int i=0;
			j=1;
			while(i<singleList.size())
			{

				Enroll eTmp=(Enroll)singleList.get(i);
				Course c=XmlOperations.getInstance().searchCourseByNo(Integer.toString(eTmp.getCode()));
				
				
				if(ProgramUitl.DEPTH_WITH_PRAC==depth)
				{											
					rows[2][j]=Integer.toString(eTmp.getMarksAssignment1());//ProgramUitl.MARK_ASSIGN_1_TEXT;
					rows[3][j]=Integer.toString(eTmp.getMarksAssignment2());//ProgramUitl.MARK_ASSIGN_2_TEXT;
					rows[4][j]=Integer.toString(eTmp.getMarksAssignment3());//ProgramUitl.MARK_ASSIGN_3_TEXT;
					rows[5][j]=Integer.toString(eTmp.getMarksClassTest());//ProgramUitl.MARK_CLASS_TEST_TEXT;
					rows[6][j]=Integer.toString(eTmp.getMarksPractical());//ProgramUitl.MARK_FINAL_PAPER_TEXT;
					rows[7][j]=Integer.toString(eTmp.getMarksFinalPaper());//ProgramUitl.MARK_PROJECT_INTERIM_REPORT_TEXT;						

					rows[2][j+1]=Integer.toString(eTmp.getMarksAssignment1Repeat());//ProgramUitl.MARK_ASSIGN_1_TEXT;
					rows[3][j+1]=Integer.toString(eTmp.getMarksAssignment2Repeat());//ProgramUitl.MARK_ASSIGN_2_TEXT;
					rows[4][j+1]=Integer.toString(eTmp.getMarksAssignment3Repeat());//ProgramUitl.MARK_ASSIGN_3_TEXT;
					rows[5][j+1]=Integer.toString(eTmp.getMarksClassTestRepeat());//ProgramUitl.MARK_CLASS_TEST_TEXT;
					rows[6][j+1]=Integer.toString(eTmp.getMarksPracticalRepeat());//ProgramUitl.MARK_FINAL_PAPER_TEXT;
					rows[7][j+1]=Integer.toString(eTmp.getMarksFinalPaperRepeat());//ProgramUitl.MARK_PROJECT_INTERIM_REPORT_TEXT;												
				}
				else
				{			
					rows[2][j]=Integer.toString(eTmp.getMarksAssignment1());//ProgramUitl.MARK_ASSIGN_1_TEXT;
					rows[3][j]=Integer.toString(eTmp.getMarksAssignment2());//ProgramUitl.MARK_ASSIGN_2_TEXT;
					rows[4][j]=Integer.toString(eTmp.getMarksAssignment3());//ProgramUitl.MARK_ASSIGN_3_TEXT;
					rows[5][j]=Integer.toString(eTmp.getMarksClassTest());//ProgramUitl.MARK_CLASS_TEST_TEXT;
					rows[6][j]=Integer.toString(eTmp.getMarksFinalPaper());//ProgramUitl.MARK_FINAL_PAPER_TEXT;
					rows[7][j]=Integer.toString(eTmp.getMarksInterimReport());//ProgramUitl.MARK_PROJECT_INTERIM_REPORT_TEXT;
					rows[8][j]=Integer.toString(eTmp.getMarksInterimPresentation());//ProgramUitl.MARK_PROJECT_INTERIM_PRESENTATION_TEXT;
					rows[9][j]=Integer.toString(eTmp.getMarksFinalReport());//ProgramUitl.MARK_PROJECT_FINAL_REPORT_TEXT;
					rows[10][j]=Integer.toString(eTmp.getMarksFinalPresentation());//ProgramUitl.MARK_PROJECT_FINAL_PRESENTATION_TEXT;
					rows[11][j]=Integer.toString(eTmp.getMarksFinalProduct());//ProgramUitl.MARK_PROJECT_FINAL_PRODUCT_TEXT;	
					
					rows[2][j+1]=Integer.toString(eTmp.getMarksAssignment1Repeat());//ProgramUitl.MARK_ASSIGN_1_TEXT;
					rows[3][j+1]=Integer.toString(eTmp.getMarksAssignment2Repeat());//ProgramUitl.MARK_ASSIGN_2_TEXT;
					rows[4][j+1]=Integer.toString(eTmp.getMarksAssignment3Repeat());//ProgramUitl.MARK_ASSIGN_3_TEXT;
					rows[5][j+1]=Integer.toString(eTmp.getMarksClassTestRepeat());//ProgramUitl.MARK_CLASS_TEST_TEXT;
					rows[6][j+1]=Integer.toString(eTmp.getMarksFinalPaperRepeat());//ProgramUitl.MARK_FINAL_PAPER_TEXT;
					rows[7][j+1]=Integer.toString(eTmp.getMarksInterimReportRepeat());//ProgramUitl.MARK_PROJECT_INTERIM_REPORT_TEXT;
					rows[8][j+1]=Integer.toString(eTmp.getMarksInterimPresentationRepeat());//ProgramUitl.MARK_PROJECT_INTERIM_PRESENTATION_TEXT;
					rows[9][j+1]=Integer.toString(eTmp.getMarksFinalReportRepeat());//ProgramUitl.MARK_PROJECT_FINAL_REPORT_TEXT;
					rows[10][j+1]=Integer.toString(eTmp.getMarksFinalPresentationRepeat());//ProgramUitl.MARK_PROJECT_FINAL_PRESENTATION_TEXT;
					rows[11][j+1]=Integer.toString(eTmp.getMarksFinalProductRepeat());//ProgramUitl.MARK_PROJECT_FINAL_PRODUCT_TEXT;							
				}
				j+=2;
				i++;				
			}
		}
			
		dtm.setDataVector(rows,cols);  
		jTResult = new JTable(dtm);		
		jTResult.setEnabled(false);
		return true;
	}
	
	java.util.List<Enroll> getASingleList(java.util.List<Enroll> inputList,java.util.List<Enroll> outputList)
	{
		if(inputList==null)
		{
			return outputList;
		}
		for(int i=0;i<inputList.size();i++)
		{
			outputList.add((Enroll)inputList.get(i));
		}
		return outputList;
	}
	
	void exit() 
	{ 		
		int confirmed = JOptionPane.showConfirmDialog(null, 
				ProgramUitl.EXIT_PRORAM_MSG, ProgramUitl.VIEW_RESULT_SHEET_TEXT, 
				JOptionPane.YES_NO_OPTION); 
				
		if (confirmed == JOptionPane.YES_OPTION) 
		{        
				ResultSheetOperation.getInstance().toggleAllButtonsStatus(true);	
				rg=null;				
				this.dispose();				
		} 
	}	
	//
	
	//Events
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource().equals(btnExit))
		{
			exit();
		}		
	}	
	//	
}